var validator;
$(function() {
	validateEditForm();
	searchData();
	
	$("#submit").click(function(){
		if ($("#keepAccountForm").valid()) {//校验表单
			validator.resetForm();
			$.ajax({
				 type : "POST",
				 url : 'newOrUpdateTransferAccountsDetail.do',
				 data : $('#keepAccountForm').serialize(),//
				 dataType : 'json',
				 success : function(response, ioArgs) {
					 $("#clear").click();
					 hiOverAlert(response.OPERATOR_RESULT);
					 detailList.query({
							click : false
						});
				}
			});
		}
	});

	
	/**
	 * 重置
	 */
	 $("#clear").click(function(){
		 $("#reset").click();
		 validator.resetForm();
	 });
	 
});


function searchData() {
	detailList = $('#tableDiv').grid(
		{
			url : "findTransferAccountDetails.do",
			root : 'detailList',
			paging:true,
			defaultParams : {
				click : true
			},
			fields : [
					{id : "fromAccount", subattr : "accountName",name : '转出账户'},
					{id : "outAmount",name : '转出金额'},
					{id : "toAccount",subattr : "accountName", name : '转入账户'},
					{id : "inAmount", name : '转入金额'},
					{id : "happenedDate",name : '日期',type:'time'},
					{id : "remark",name : '备注'},
					
					{name : "操作",style : {'text-align' : 'center'},
						render : function(data) {
							var box = $("<span/>");
							//编辑
							var edit=$('<a />').attr("href","javascript:void(0)").attr('class','button_a').append($('<span />').html("编辑  ")).bind('click',function(){
								$('#detailId').val(data.id);
								$('#editFromAccount').val(data.fromAccount.id);
								$('#editOutAmount').val(data.outAmount);
								$('#editToAccount').val(data.toAccount.id);
								$('#editInAmount').val(data.inAmount);
								$('#happenedDate').val(data.happenedDate);
								$('#remark').val(data.remark);
							}); 	
							edit.appendTo(box);
								
							//删除
							var deletaObj = $('<a />').attr("href", "#").attr('class', 'button_a').append($('<span />').html("删除  ")).bind(
							'click',function() {
								hiConfirm('确认删除该记录？', '提示', function(flag) {
									if(flag){
										$.ajax({
											type : "POST",
											url : 'deleteTransferAccountsDetail.do',
											data : {
												'id' : data.id
											},
											dataType : 'json',
											success : function(response,ioArgs) {
												hiOverAlert(response.OPERATOR_RESULT);
												detailList.query({
													click : false
												});
											}	
										});
								    }
								});
							});

						    deletaObj.appendTo(box);
							return box;

						}
					} ]
		});
}

/**
 * 校验规则
 */
function validateEditForm(){  
	 validator= $("#keepAccountForm").validate({  
		 success:'checked',
		 rules: {
			 'detail.outAmount': {
				 required:true,
				 number:true,	    	
				 rangelength:[0,10]
			 },
			 'detail.toAccount.id': {
				 checkAccountSame:true
			 },
			 'detail.inAmount': {
				 required:true,
				 number:true,	    	
				 rangelength:[0,10]
			 },
			 'detail.happenedDate': {   
				 required:true
			 }
		 },
		 messages:{			
			 'detail.outAmount': {	
				 required:"请填写金额！",
				 number:"金额只能为数字！",	    	
				 rangelength:"长度在10以内！"
			 },
			 'detail.toAccount.id': {
				 
			 },
			 'detail.inAmount': {	
				 required:"请填写金额！",
				 number:"金额只能为数字！",	    	
				 rangelength:"长度在10以内！"
			 },
			 'detail.happenedDate': {   
				 required:"请填写日期！"
			 }
		 }
	 });
} 

$.validator.addMethod("checkAccountSame", function(value,element,param) {//自定义验证,验证编号是否存在
	 if($("#editFromAccount").val()==$("#editToAccount").val()){//更改后的编号和原始编号一致，则不需要验证
		 return false;
		 }
	 return true;
	 },"转入与转出的账户不能相同！");